package in.muton.edushare.grading.panels;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.grading.panels.studentGrades.StudentMainGradePanel;
import in.muton.edushare.pages.assignment.Edit;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.coursepages.CourseGrading;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.GradingItem;

public class GradeItemsList extends EdusharePanel {
	private static final long serialVersionUID = 2173102596678692393L;

	public GradeItemsList(String id, final CourseGrading page,
			IModel<CourseInstances> cInst, final NewGradeItemPanel ngip,
			final StudentMainGradePanel smgp) {
		super(id, page);
		this.cInst = cInst;
		add(new Link<Void>("addComponent") {
			private static final long serialVersionUID = 2743497172298363799L;

			@Override
			public boolean isVisible() {
				CourseRoleCheck crc = new CourseRoleCheck(GradeItemsList.this.cInst.getObject(), getLoggedInUser());
				return crc.isAdmin()||crc.isTeacher();
			}

			@Override
			public void onClick() {
				page.setActivePanel(CourseGrading.NEW_GRADE_COMPONENT);
			}
		});

		add(new ListView<IModel<GradingItem>>("gradeItemLV",
				new InnerList<GradingItem>() {
					private static final long serialVersionUID = -1605356161710511517L;
					@Override
					public List<IModel<GradingItem>> getObject() {
						return new CayenneList<GradingItem>(
								GradeItemsList.this.cInst.getObject()
										.getGradingItems()).getObject();
					}
				}) {
			private static final long serialVersionUID = -4435118195965121339L;

			@Override
			protected void populateItem(final ListItem<IModel<GradingItem>> arg0) {
				GradingItem g = arg0.getModelObject().getObject();
				arg0.add(new Label("gradeItemName", g.getGradingItemName()));
				arg0.add(new Label("gradeItemType", g.getItemTypeString()));
				arg0.add(new Label("maxMarks", "" + g.getMaxMarks()));
				arg0.add(new Label("weightage", "" + g.getWeightage()+"%"));

				arg0.add(new Link<Void>("editLink") {
					private static final long serialVersionUID = -5102126787021366481L;
					@Override
					public boolean isVisible(){
						CourseRoleCheck crc = new CourseRoleCheck(GradeItemsList.this.cInst.getObject(), getLoggedInUser());
						return crc.isAdmin()||crc.isTeacher();						
					}
					
					@Override
					public void onClick() {
						GradingItem g = arg0.getModelObject().getObject();
						if(g.getItemType() != GradingItem.ITEM_COURSE_ASSGN){
							ngip.setgItem(arg0.getModelObject());
							page.setActivePanel(CourseGrading.NEW_GRADE_COMPONENT);
						}else{
							PageParameters params = new PageParameters();
							params.put("0", g.getAssignment().getAssignmentId().toString());
							Edit editPage = new Edit(params);
							setResponsePage(editPage);
						}
					}
				});

				arg0.add(new Link<Void>("viewGrades") {
					private static final long serialVersionUID = -5102126787021366481L;

					@Override
					public void onClick() {
						smgp.setiGradeModel(arg0.getModelObject());
						page.setActivePanel(CourseGrading.COURSE_GRADES);
					}
				});
			}
		});
	}

	private IModel<CourseInstances> cInst = null;
}
